﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;

using Microsoft.VisualStudio.VCProjectEngine;
using EnvDTE;
using iMainAddin;
using iSystem;

namespace iProjectAddin
{
    public class iVcModuleProject : iVcProject
    {
        ~iVcModuleProject()
        {
        }
        public override bool Init()
        {
            return true;
        }
        public override bool Create(Object createValue)
        {
            base.Create(createValue);

            String name = createValue as String;
            if ( name == null || name.Length == 0 )
                return false;

            Solution CurrentSolution = (Solution)Application.Solution;
            try
            {
                String sSolutionPath = Path.GetDirectoryName(CurrentSolution.FullName) + @"\";

                String currentDirctory = Directory.GetCurrentDirectory();
                Directory.SetCurrentDirectory(sSolutionPath);
                String sBaseProjectPaht = @"..\..\Project\" + Path.GetFileNameWithoutExtension(CurrentSolution.FullName)
                    + @"\" + name + @"\Win32\VC90\";
                System.IO.DirectoryInfo dirInfo = Directory.CreateDirectory(sBaseProjectPaht);
                Directory.SetCurrentDirectory(currentDirctory);

                String sProjectPath = AddintDataPath + @"ModuleProject_9\ModlueProject.vcproj";
                Project newProject = CurrentSolution.AddFromFile(
                    sProjectPath, false);
				if( SetVCProject(newProject.Object) == null )
                {
                    return false;
                }

                VCProjcet.RootNamespace = name;
                VCProjcet.Name = name;
                newProject.SaveAs(dirInfo.FullName + name + ".vcproj");
                CurrentSolution.SaveAs(CurrentSolution.FullName);

				IEnumerator rator = newProject.ProjectItems.GetEnumerator();
				ProjectItemCopy(rator);

				IVCCollection collection = VCProjcet.Configurations as IVCCollection;
				rator = collection.GetEnumerator();
                ProjectPrpertyCopy(sSolutionPath, name, rator);

                newProject.Save(dirInfo.FullName + name + ".vcproj");
				CurrentSolution.SaveAs(CurrentSolution.FullName);
            }
            catch (SystemException e)
            {
                MessageBox.Show("ERROR: " + e.Message);
                return false;
            }
            return true;
        }
		
        protected void ProjectPrpertyCopy(String solutionPath, String projectName,
            IEnumerator configurationRator)
		{
			while(configurationRator.MoveNext())
			{
                VCConfiguration configuration = configurationRator.Current as VCConfiguration;
                if (configuration == null)
					continue;

                if (configuration.Name.Contains("DLL"))
                {
                    IVCCollection tools = (IVCCollection)configuration.Tools;
                    VCCLCompilerTool compilerTool = (VCCLCompilerTool)tools.Item("VCCLCompilerTool");
                    String define = compilerTool.PreprocessorDefinitions;
                    String exportName = projectName.ToUpper() + "_EXPORT;";
                    compilerTool.PreprocessorDefinitions = exportName + define;
                }
                

				String InheritedPropertySheets = null;
                IVCCollection prpertyCollection = configuration.PropertySheets as IVCCollection;
				IEnumerator seetRator = prpertyCollection.GetEnumerator();
				while (seetRator.MoveNext())
				{
					VCPropertySheet seet = seetRator.Current as VCPropertySheet;
					if (seet == null)
						continue;
					seet.PropertySheetFile = solutionPath + @"Property Seet\" + seet.Name + @".vsprops";
					InheritedPropertySheets += seet.PropertySheetFile;
					//seet.Save();
				}
                configuration.InheritedPropertySheets = InheritedPropertySheets;
			}
		}

        protected void ProjectItemCopy(IEnumerator rator)
        {
            ProjectItem item;
			while (rator.MoveNext())
			{
			    item = rator.Current as ProjectItem;

			    if (item.Kind == "{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}") // 물리적 파일
			    {
					String sNewFilePath = VCProjcet.ProjectDirectory + VCProjcet.Name + item.Name;
			        String sBaseFilePath = item.get_FileNames(1);
			        String baseName = iSystem.iFileAssist.RemoveExtension(item.Name);
					String sNewName = VCProjcet.Name + item.Name;

                    File.Copy(sBaseFilePath, sNewFilePath);

					// 파일 위치 변경
					Property Exe = (Property)item.Properties.Item("Extension");
			        Property property = (Property)item.Properties.Item("RelativePath"); // 11
			        property.Value = sNewFilePath;
					item.Name = sNewName;

					// 문서 변경(지정어 변경)
					item.Open(Constants.vsViewKindCode);
                    
                    String fileName = iSystem.iFileAssist.RemoveExtension(sNewName);

                    iProjectDoucment changeFile = new iProjectDoucment();
                    changeFile.AddDictionary("$(ProjectName)", VCProjcet.Name);
                    changeFile.AddDictionary("$(ProjectNameUP)", VCProjcet.Name.ToUpper());
                    changeFile.AddDictionary("$(FileName)", fileName);
                    changeFile.AddDictionary("$(FileNameUP)", fileName.ToUpper());
                    changeFile.StartChange(item);
                    //iModuleDoucmentItem documentItem 
                    //    = new iModuleDoucmentItem(m_vcProjcet.Name, iSystem.iFileAssist.RemoveExtension(sNewName), item);
					
			    }
				else  // (item.Kind == "{6BB5F8F0-4483-11D3-8BCF-00C04F8EC28C}") // 프로젝트 파일
				{
					ProjectItemCopy(item.ProjectItems.GetEnumerator());
					continue;
                }
			}
        }
    }
}